package ae.pcfc.cldos.online.net.service.provider;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.client.core.WebServiceMessageCallback;
import org.springframework.ws.soap.SoapMessage;

import ae.pcfc.cldos.online.net.ConstantsNET;
import ae.pcfc.cldos.online.net.service.GSEmployeeDetailsService;
import ae.pcfc.cldos.online.net.service.types.ApproveVisitVisaCancel;
import ae.pcfc.cldos.online.net.service.types.ApproveVisitVisaCancelResponse;
import ae.pcfc.cldos.online.net.service.types.DTONoc;
import ae.pcfc.cldos.online.net.service.types.EmployeDetails;
import ae.pcfc.cldos.online.net.service.types.EmployePaging;
import ae.pcfc.cldos.online.net.service.types.GetDTONoc;
import ae.pcfc.cldos.online.net.service.types.GetDTONocResponse;
import ae.pcfc.cldos.online.net.service.types.GetEmployeeByEmployeeID;
import ae.pcfc.cldos.online.net.service.types.GetEmployeeByEmployeeIDResponse;
import ae.pcfc.cldos.online.net.service.types.GetEmployeeStatus;
import ae.pcfc.cldos.online.net.service.types.GetEmployeeStatusResponse;
import ae.pcfc.cldos.online.net.service.types.GetEmployees;
import ae.pcfc.cldos.online.net.service.types.GetEmployeesResponse;
import ae.pcfc.cldos.online.net.service.types.GetVisitorByVisitorIDCompanyLicenseNo;
import ae.pcfc.cldos.online.net.service.types.GetVisitorByVisitorIDCompanyLicenseNoResponse;
import ae.pcfc.cldos.online.net.service.types.GetVisitors;
import ae.pcfc.cldos.online.net.service.types.GetVisitorsResponse;
import ae.pcfc.cldos.online.net.service.types.InactiveFinanceDataStatus;
import ae.pcfc.cldos.online.net.service.types.InactiveFinanceDataStatusResponse;
import ae.pcfc.cldos.online.net.service.types.InsertFinancialData;
import ae.pcfc.cldos.online.net.service.types.InsertFinancialDataResponse;
import ae.pcfc.cldos.online.net.service.types.IsVisitorsExistByVisitorIDCompanyLicenseNo;
import ae.pcfc.cldos.online.net.service.types.IsVisitorsExistByVisitorIDCompanyLicenseNoResponse;
import ae.pcfc.cldos.online.net.service.types.ValidateEmployeeIDWithLicenceNo;
import ae.pcfc.cldos.online.net.service.types.ValidateEmployeeIDWithLicenceNoResponse;
import ae.pcfc.cldos.online.net.service.vo.EmployeeDetailsFromGSVO;
import ae.pcfc.cldos.online.net.service.vo.InsertVoucherVO;
import ae.pcfc.cldos.online.net.service.vo.ListOfEmployeeDetailsFromGSVO;
import ae.pcfc.cldos.online.net.service.vo.NOCDetailsFromGSVO;
import ae.pcfc.cldos.online.ws.base.BaseWebService;

/**
 * 
 * @author arosa
 *
 */
public class GSEmployeeDetailsServiceImpl extends BaseWebService implements GSEmployeeDetailsService {

	@Value("${net.url}")
	String classURIValue = ConstantsNET.NET_WS_ENDPOINT;
	
	public ListOfEmployeeDetailsFromGSVO getEmployeeDetailsList(String companyLicenseNo, Integer page, Integer perPage){
		GetEmployees getEmployees = new GetEmployees();
		getEmployees.setCompanyLicenseNo(companyLicenseNo);
		getEmployees.setPageNum(page);
		getEmployees.setPageSize(perPage);
		
		GetEmployeesResponse response = (GetEmployeesResponse) wsTemplate
				.marshalSendAndReceive(classURIValue, getEmployees,
						new WebServiceMessageCallback() {
							public void doWithMessage(WebServiceMessage message) {
								((SoapMessage) message)
										.setSoapAction(ConstantsNET.GET_EMPLOYEE_LIST);
							}
						});
		List<EmployePaging> employePagingsList = response.getGetEmployeesResult().getEmployeePagList().getEmployePaging();
		List<EmployeeDetailsFromGSVO> empDetFromGs = new ArrayList<EmployeeDetailsFromGSVO>();
		for(EmployePaging employePaging : employePagingsList){
			EmployeeDetailsFromGSVO employeeDetailsFromGSVO = new EmployeeDetailsFromGSVO();
			getMapper().map(employePaging, employeeDetailsFromGSVO);
			empDetFromGs.add(employeeDetailsFromGSVO);
		}
		
		Integer total = response.getGetEmployeesResult().getTotalRowNum();
		ListOfEmployeeDetailsFromGSVO listOfEmployeeDetailsFromGSVO = new ListOfEmployeeDetailsFromGSVO();
		listOfEmployeeDetailsFromGSVO.setEmployeeDetailsFromGSVOList(empDetFromGs);
		listOfEmployeeDetailsFromGSVO.setTotal(total);
		
		return listOfEmployeeDetailsFromGSVO;
	}
	
	public EmployeeDetailsFromGSVO getEmployeeDetailsById(String employeeId, String companyLicNo, boolean isGeneralAdminRequest){
		
		GetEmployeeByEmployeeID getEmployeeByEmployeeID = new GetEmployeeByEmployeeID();
		getEmployeeByEmployeeID.setCompanyLicenseNo(companyLicNo);
		getEmployeeByEmployeeID.setEmployeeID(employeeId);
		getEmployeeByEmployeeID.setSkipCompanyLicenseNo(isGeneralAdminRequest);
		
		GetEmployeeByEmployeeIDResponse response = (GetEmployeeByEmployeeIDResponse) wsTemplate
				.marshalSendAndReceive(classURIValue, getEmployeeByEmployeeID,
						new WebServiceMessageCallback() {
							public void doWithMessage(WebServiceMessage message) {
								((SoapMessage) message)
										.setSoapAction(ConstantsNET.GET_EMPLOYEE_BY_ID);
							}
						});
		
		EmployeDetails employeDetails = response.getGetEmployeeByEmployeeIDResult();
		EmployeeDetailsFromGSVO employeeDetailsFromGSVO = new EmployeeDetailsFromGSVO();
		getMapper().map(employeDetails, employeeDetailsFromGSVO);
		
		return employeeDetailsFromGSVO;
	}
	
	public boolean validateEmployee(String employeeId, String companyLicNo){
		ValidateEmployeeIDWithLicenceNo validateEmployeeIDWithLicenceNo = new ValidateEmployeeIDWithLicenceNo();
		validateEmployeeIDWithLicenceNo.setCompanyLicenseNo(companyLicNo);
		validateEmployeeIDWithLicenceNo.setEmployeeID(employeeId);
		
		ValidateEmployeeIDWithLicenceNoResponse response = (ValidateEmployeeIDWithLicenceNoResponse) wsTemplate
				.marshalSendAndReceive(classURIValue, validateEmployeeIDWithLicenceNo,
						new WebServiceMessageCallback() {
							public void doWithMessage(WebServiceMessage message) {
								((SoapMessage) message)
										.setSoapAction(ConstantsNET.CHECK_EMPLOYEE_BY_ID);
							}
						});
		
		return response.isValidateEmployeeIDWithLicenceNoResult();
	}
	
	public ListOfEmployeeDetailsFromGSVO getVisitorsDetailsList(String companyLicenseNo, Integer page, Integer perPage){
		GetVisitors getVisitors = new GetVisitors();
		getVisitors.setCompanyLicenseNo(companyLicenseNo);
		getVisitors.setPageNum(page);
		getVisitors.setPageSize(perPage);
		
		GetVisitorsResponse response = (GetVisitorsResponse) wsTemplate
				.marshalSendAndReceive(classURIValue, getVisitors,
						new WebServiceMessageCallback() {
							public void doWithMessage(WebServiceMessage message) {
								((SoapMessage) message)
										.setSoapAction(ConstantsNET.GET_VISITORS_LIST);
							}
						});
		List<EmployePaging> employePagingsList = response.getGetVisitorsResult().getEmployeePagList().getEmployePaging();
		List<EmployeeDetailsFromGSVO> empDetFromGs = new ArrayList<EmployeeDetailsFromGSVO>();
		for(EmployePaging employePaging : employePagingsList){
			EmployeeDetailsFromGSVO employeeDetailsFromGSVO = new EmployeeDetailsFromGSVO();
			getMapper().map(employePaging, employeeDetailsFromGSVO);
			empDetFromGs.add(employeeDetailsFromGSVO);
		}
		
		Integer total = response.getGetVisitorsResult().getTotalRowNum();
		ListOfEmployeeDetailsFromGSVO listOfEmployeeDetailsFromGSVO = new ListOfEmployeeDetailsFromGSVO();
		listOfEmployeeDetailsFromGSVO.setEmployeeDetailsFromGSVOList(empDetFromGs);
		listOfEmployeeDetailsFromGSVO.setTotal(total);
		
		return listOfEmployeeDetailsFromGSVO;
	}
	
	public boolean validateVisitor(String visitorId, String companyLicNo){
		IsVisitorsExistByVisitorIDCompanyLicenseNo isVisitorsExistByVisitorIDCompanyLicenseNo = 
				new IsVisitorsExistByVisitorIDCompanyLicenseNo();
		isVisitorsExistByVisitorIDCompanyLicenseNo.setCompanyLicenseNo(companyLicNo);
		isVisitorsExistByVisitorIDCompanyLicenseNo.setVisitorID(visitorId);
		
		IsVisitorsExistByVisitorIDCompanyLicenseNoResponse response = 
				(IsVisitorsExistByVisitorIDCompanyLicenseNoResponse) wsTemplate
				.marshalSendAndReceive(classURIValue, isVisitorsExistByVisitorIDCompanyLicenseNo,
						new WebServiceMessageCallback() {
							public void doWithMessage(WebServiceMessage message) {
								((SoapMessage) message)
										.setSoapAction(ConstantsNET.CHECK_VISITOR_BY_ID);
							}
						});
		
		return response.isIsVisitorsExistByVisitorIDCompanyLicenseNoResult();
	}
	
	public EmployeeDetailsFromGSVO getVisitorDetailsById(String visitorId, String companyLicNo, boolean isGeneralAdminRequest){
		GetVisitorByVisitorIDCompanyLicenseNo getVisitorByVisitorID = new GetVisitorByVisitorIDCompanyLicenseNo();
		getVisitorByVisitorID.setVisitorID(visitorId);
		getVisitorByVisitorID.setCompanyLicenseNo(companyLicNo);
		getVisitorByVisitorID.setSkipCompanyLicenseNo(isGeneralAdminRequest);
		
		GetVisitorByVisitorIDCompanyLicenseNoResponse response = (GetVisitorByVisitorIDCompanyLicenseNoResponse) wsTemplate
				.marshalSendAndReceive(classURIValue, getVisitorByVisitorID,
						new WebServiceMessageCallback() {
							public void doWithMessage(WebServiceMessage message) {
								((SoapMessage) message)
										.setSoapAction(ConstantsNET.GET_VISITOR_BY_ID);
							}
						});
		
		EmployeDetails employeDetails = response.getGetVisitorByVisitorIDCompanyLicenseNoResult();
		EmployeeDetailsFromGSVO employeeDetailsFromGSVO = new EmployeeDetailsFromGSVO();
		getMapper().map(employeDetails, employeeDetailsFromGSVO);
		
		return employeeDetailsFromGSVO;
	}
	
	/**
	 * @param empId
	 * @param companyLicNo
	 * @return
	 */
	public NOCDetailsFromGSVO getNOCDetailsById(String empId, String companyLicNo,String appId){
		GetDTONoc getDTONoc = new GetDTONoc();
		getDTONoc.setEmployeeID(empId);
		getDTONoc.setCompanyLicenseNo(companyLicNo);
		getDTONoc.setAppId(appId);
		
		GetDTONocResponse response = (GetDTONocResponse) wsTemplate
				.marshalSendAndReceive(classURIValue, getDTONoc,
						new WebServiceMessageCallback() {
							public void doWithMessage(WebServiceMessage message) {
								((SoapMessage) message)
										.setSoapAction(ConstantsNET.GET_NOC_DETAILS);
							}
						});
		
		NOCDetailsFromGSVO nocDetailsFromGSVO = new NOCDetailsFromGSVO();
		DTONoc dtoNoc  = response.getGetDTONocResult();
		getMapper().map(dtoNoc, nocDetailsFromGSVO);
		return nocDetailsFromGSVO;
	}
	/**
	 * @param clientRefNo
	 * @return empstatus
	 */
	public String getEmployeeStatus(String clientRefNo){
		GetEmployeeStatus getEmployeeStatus = new GetEmployeeStatus();
		getEmployeeStatus.setOfflineAppId(clientRefNo);
		GetEmployeeStatusResponse response = (GetEmployeeStatusResponse) wsTemplate
				.marshalSendAndReceive(classURIValue,getEmployeeStatus,
						new WebServiceMessageCallback() {
							public void doWithMessage(WebServiceMessage message) {
								((SoapMessage) message)
										.setSoapAction(ConstantsNET.GET_EMP_STATUS);
							}
						});
		return response.getGetEmployeeStatusResult();
		
	}
	/**
	 * @param OfflineAppId
	 * @return
	 */
	public boolean approveVisitVisaCancel(String OfflineAppId,String appId, String licNo){
		ApproveVisitVisaCancel approveVisitVisaCancel = new ApproveVisitVisaCancel();
		approveVisitVisaCancel.setOfflineAppId(OfflineAppId);
		approveVisitVisaCancel.setCompanyLicenseNo(appId);
		approveVisitVisaCancel.setOnlineAppId(licNo);
		
		ApproveVisitVisaCancelResponse response = (ApproveVisitVisaCancelResponse) wsTemplate
				.marshalSendAndReceive(classURIValue, approveVisitVisaCancel,
						new WebServiceMessageCallback() {
							public void doWithMessage(WebServiceMessage message) {
								((SoapMessage) message)
										.setSoapAction(ConstantsNET.APPROVE_VV_CANCEL);
							}
						});
		
		return response.isApproveVisitVisaCancelResult();
	}
	
	/**
	 * @param insertVoucherVO
	 */
	public void insertPaymentAdvice(InsertVoucherVO insertVoucherVO){
		InsertFinancialData insertFinancialData = new InsertFinancialData();
		insertFinancialData.setAdivceCreatedBy(insertVoucherVO.getAdivceCreatedBy());
		insertFinancialData.setAdviceId(insertVoucherVO.getAdviceId());
		insertFinancialData.setApplicationId(insertVoucherVO.getApplicationId());
		insertFinancialData.setProcessStepCode(insertVoucherVO.getProcessStepCode());
		
		InsertFinancialDataResponse response = (InsertFinancialDataResponse) wsTemplate
				.marshalSendAndReceive(classURIValue, insertFinancialData,
						new WebServiceMessageCallback() {
							public void doWithMessage(WebServiceMessage message) {
								((SoapMessage) message)
										.setSoapAction(ConstantsNET.INSERT_PAYMENT_ADVICE);
							}
						});
		
	}
	
	/**
	 * @param OfflineAppId
	 * @param adviceId
	 */
	public void deactivatePaymentAdvice(String OfflineAppId, String adviceId){
		InactiveFinanceDataStatus inactiveFinanceDataStatus = new InactiveFinanceDataStatus();
		inactiveFinanceDataStatus.setAdviceId(adviceId);
		inactiveFinanceDataStatus.setApplicationId(OfflineAppId);
		
		InactiveFinanceDataStatusResponse response = (InactiveFinanceDataStatusResponse) wsTemplate
				.marshalSendAndReceive(classURIValue, inactiveFinanceDataStatus,
						new WebServiceMessageCallback() {
							public void doWithMessage(WebServiceMessage message) {
								((SoapMessage) message)
										.setSoapAction(ConstantsNET.INACTIVATE_PAYMENT_ADVICE);
							}
						});
	}
}
